package com.example.demo.component;


import com.example.demo.UserService;
import com.example.demo.model.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/test")
public class UserController {
//    @Autowired
//    private ApplicationContext context; //拿到的便是ApplicationContext context = SpringApplication.run(Springioc2Application.class, args);的对象
    //依赖注入有赋值的动作   (绑定赋值)
    //1.属性注入
//    @Autowired  //没有加@Autowired 就是一个普通的属性声明,下面 userService.sayHi();会报错
//    private UserService userService;

    //2.Setter方法注入
//    private UserService userService;
////    @Autowired
//    public UserController(UserService userService) {
//        this.userService = userService;
//    }

    //3.构造方法注入
//    private  final  UserService userService = new UserService();//final修饰的要进行初始化 1.new 2.构造函数-----用上了构造函数,那还不如直接构造函数注入了
//    @Autowired
//    private UserService userService;
//    @Qualifier("u2")
//    @Resource(name = "userInfo2")
    @Autowired
    private UserInfo u1;
    @RequestMapping("/t1")
    public String test(){
//        System.out.println(u1);
        return "111";
    }
//@Autowired

//有了无参的构造方法时，会报空指针异常
//    public UserController() {
//    }
//    @Autowired
//    public UserController(UserService userService) {
//        this.userService = userService;
//    }

    //1和2有什么区别吗?
    //2可以调用方法,通过修改参数可以换


    public void sayHi() {
        System.out.println("Hi,UserController...");
//        userService.sayHi(); //如果没有 @Autowired,这会报空指针异常
        System.out.println("????????????");
        System.out.println(u1);
    }

}
